package com.fhao.forum.controller;

import com.fhao.forum.common.AppResult;
import com.fhao.forum.common.ResultCode;
import com.fhao.forum.exception.ApplicationException;
import com.fhao.forum.model.Board;
import com.fhao.forum.services.IBoardService;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@ApiResponses
@RestController
@Slf4j
@RequestMapping("/board")
public class BoardController {
    @Value("${fhao-forum.index.board-num: 9}")
    private Integer indexBoardNum;
    @Resource
    private IBoardService service;

    @ApiResponse(description = "获取在⾸⻚中显⽰的版块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        //不用传参数,配置到.yml文件中注入过来即可
        log.info("首页版块个数: "+indexBoardNum);
        //调用service查询结果
        List<Board> boards = service.selectByNum(indexBoardNum);
        //对结果进行校验
        if(boards == null) {
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @ApiResponse(description = "根据id获取版块信息")
    @GetMapping("/getById")
    public AppResult<Board> getById(@RequestParam("id") @NonNull Long id) {
        Board board = service.selectById(id);
        if(board == null || board.getDeleteState() == 1) {
            log.warn("BoardController getById, "+ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //返回结果
        return AppResult.success(board);
    }
}


















